<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/1/31
 * Time: 17:10
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Supplier extends Base
{
    /**
     * 供应商首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $supplier_name = input('supplier_name');
            $supplier_status = input('supplier_status');
            $supplier_email = input('supplier_email');
            $where['supplier_status'] = ['BETWEEN', [1, 2]];

            if($supplier_status) $where['supplier_status'] = $supplier_status;
            if($supplier_name) $where['supplier_name'] = ['LIKE', '%' . $supplier_name . '%'];
            if($supplier_email) $where['supplier_email'] = ['LIKE', '%' . $supplier_email . '%'];

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_supplier')->where($where)->count();// 获取总条数
            $lists = Db::table('up_supplier')
                ->where($where)
                ->field('supplier_id, supplier_name, supplier_status, supplier_time, 
                supplier_email, supplier_username, supplier_phone, supplier_address, supplier_code')
                ->page($Nowpage, $limits)
                ->order('supplier_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 添加供应商
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function supplierAdd(){
        if(Request::instance()->isPost()){
            $supplier_name = input('supplier_name');
            $supplier_status = input('supplier_status');
            $supplier_email = input('supplier_email');
            $supplier_username = input('supplier_username');
            $supplier_phone = input('supplier_phone');
            $supplier_address = input('supplier_address');

            $has = Db::table('up_supplier')->where(['supplier_name' => $supplier_name])->find();
            if($has) return json(['code' => 100, 'msg' => '该供应商已存在']);

            $exist = Db::table('up_supplier')->order('supplier_id desc')->limit(1)->find();
            if($exist){
                $code = $exist['supplier_code'] + 1;
            }else{
                $code = 1000;
            }

            $insert = [
                'supplier_name' => $supplier_name,
                'supplier_code' => $code,
                'supplier_status' => $supplier_status,
                'supplier_email' => $supplier_email,
                'supplier_username' => $supplier_username,
                'supplier_phone' => $supplier_phone,
                'supplier_address' => $supplier_address,
                'supplier_time' => initTime()
            ];

            $res = Db::table('up_supplier')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑供应商
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function supplierEdit(){
        $supplier_id = input('supplier_id');
        if(Request::instance()->isPost()){
            $supplier_name = input('supplier_name');
            $supplier_status = input('supplier_status');
            $supplier_email = input('supplier_email');
            $supplier_username = input('supplier_username');
            $supplier_phone = input('supplier_phone');
            $supplier_address = input('supplier_address');

            $has = Db::table('up_supplier')->where(['supplier_name' => $supplier_name])->find();
            if($has && ($supplier_id != $has['supplier_id'])) return json(['code' => 100, 'msg' => '该供应商已存在']);

            $update = [
                'supplier_name' => $supplier_name,
                'supplier_status' => $supplier_status,
                'supplier_email' => $supplier_email,
                'supplier_username' => $supplier_username,
                'supplier_phone' => $supplier_phone,
                'supplier_address' => $supplier_address,
                'supplier_update_time' => initTime()
            ];

            $res = Db::table('up_supplier')->where('supplier_id', $supplier_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $data = Db::table('up_supplier')
            ->where('supplier_id', $supplier_id)
            ->field('supplier_id, supplier_name, supplier_status, supplier_email, supplier_username, 
            supplier_phone, supplier_address')
            ->find();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }

    /**
     * 上架/下架
     * @return \think\response\Json
     */
    public function supplierChangeStatus(){
        $supplier_id = input('id');
        $status = Db::table('up_supplier')->where('supplier_id', $supplier_id)->value('supplier_status');
        $newStatus = $status == 1 ? 2 : 1;
        $res = Db::table('up_supplier')->where('supplier_id', $supplier_id)->setField('supplier_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function supplierDel(){
        $supplier_id = input('id');
        $res = Db::table('up_supplier')->where('supplier_id', $supplier_id)->setField('supplier_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}